package com.exam.controller;


import com.exam.entity.ApiResult;
import com.exam.entity.Student;
import com.exam.serviceimpl.StudentServiceImpl;
import com.exam.util.ApiResultHandler;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping(value = {"/studentcontroller"})
public class StudentController {

    @Autowired
    private StudentServiceImpl studentService;

    @RequestMapping("/findAll/{page}/{size}")
    public ResponseEntity<Page<Student>> findAll(@PathVariable(value = "page") Integer page, @PathVariable(value = "size") Integer size) throws Exception
    {
        Page<Student> studentPage = new Page<>(page,size);
        List<Student> studentList = this.studentService.findAll(page, size);
        return  new ResponseEntity<Page<Student>>((Page<Student>) studentList, HttpStatus.OK);
    }

    @RequestMapping("/findById/{studentId}")
    public ResponseEntity<Student> findById(@PathVariable("studentId") Integer studentId) {
        return  new ResponseEntity<Student>(studentService.findById(studentId), HttpStatus.OK);
    }

    @RequestMapping("/deleteById/{studentId}")
    public ResponseEntity<Boolean> deleteById(@PathVariable("studentId") Integer studentId) throws Exception{
        this.studentService.deleteById(studentId);
        return  new ResponseEntity<Boolean>(true, HttpStatus.OK);
    }

    @RequestMapping("/updatePwd")
    public ResponseEntity<Boolean> updatePwd(@RequestBody Student student) {
        studentService.updatePwd(student);
        return  new ResponseEntity<Boolean>(true, HttpStatus.OK);
    }
    @PostMapping("/update")
    public ResponseEntity<Boolean> update(@RequestBody Student student) {
        this.studentService.update(student);
        return  new ResponseEntity<Boolean>(true, HttpStatus.OK);
    }

    @RequestMapping("/add")
    public ResponseEntity<Boolean> add(@RequestBody Student student) {
        this.studentService.add(student);
        return  new ResponseEntity<Boolean>(true, HttpStatus.OK);
    }
}
